<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/10/15
 * Time: 15:54
 */

namespace app\api\model;


use traits\model\SoftDelete;

class WorkDepartment extends BaseModel
{
//    use SoftDelete;
//    protected $deleteTime = 'delete_time';
//    protected $updateTime = false;
    protected $hidden=['delete_time','create_time','update_time'];
    protected $autoWriteTimestamp = true;

    /**
     * 删除部门
     * @param $uid
     * @param null $d_id
     * @return int
     */
    public static function deleteDepartment($uid,$d_id=null){
        if ($d_id == null){
            $status = self::where('t_id','=',$uid)->delete();
        }else{
            $status = self::where('t_id','=',$uid)
                ->where('d_id','=',$d_id)
                ->delete();
        }
        return $status;
    }
    /**
     * 查询部门信息
     * @param $uid
     * @param $d_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function  getDepartment($uid,$d_id){
        $status = self::where('t_id','=',$uid)
            ->where('d_id','=',$d_id)
            ->find();
        return $status;
    }

    /**
     *更新部门信息
     * @param $uid
     * @param $d_id
     * @param $name
     * @return $this
     */
    public static function updateDepartment($uid,$d_id,$name){
        $status = self::where('t_id','=',$uid)
            ->where('d_id','=',$d_id)
            ->update($name);
        return $status;
    }

    /**
     * 获取部门列表
     * @param $uid
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getDepartmentList($uid){
        $result = self::where('t_id','=',$uid)->select();
        return $result;
    }

    /**
     * 获取用户部门列表
     * @param $uid
     * @param $d_ids
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getDepartmentListByUser($uid,$d_ids){
        $result = self::where('t_id','=',$uid)
                ->where('d_id','in',$d_ids)->select();
        return $result;
    }

    /**
     * 批量更新部门
     * @param $data
     * @return array|false
     */
    public static function batchUpdate($data){
        $str = '';
        foreach ($data as $value){
           $str =  self::where('t_id','=',$value['t_id'])->where('d_id','=',$value['d_id'])->update($value);
        }
        return $str;
    }
}